/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bakenote.ui;

import bakenote.BakeNote;
import bakenote.core.Procedure;
import bakenote.core.Recipe;
import bakenote.data.RecipeList;
import bakenote.xml.ProcedureXMLReader;
import org.apache.pivot.beans.BXML;
import org.apache.pivot.beans.Bindable;
import org.apache.pivot.collections.Map;
import org.apache.pivot.util.Resources;
import org.apache.pivot.wtk.*;
import org.apache.pivot.wtk.Component;
import org.apache.pivot.wtk.Label;
import org.apache.pivot.wtk.Menu;
import org.apache.pivot.wtk.TextArea;

import java.awt.Font;
import java.io.*;
import java.net.URL;

/**
 * @author lello
 */
public class RecipeDataPane extends TabPane implements Bindable {

    @BXML
    private TablePane procedureTablePane = null;
    @BXML
    private TextArea noteTextArea = null;
    @BXML
    private Label timeLabel = null;

    private ProcedureContextMenu procedureContextMenu = null;

    public RecipeDataPane() {
    }

    public void setRecipeData(Recipe recipe) {

        if (recipe.getNote() != null) {
            // Load notes
            noteTextArea.setText(recipe.getNote());
        }
        // Load procedure
        Procedure p = recipe.getProcedure();
        if (!(p == null)) {
            int len = procedureTablePane.getRows().getLength();
            procedureTablePane.getRows().remove(0, len);
            for (int i = 0; i < p.getElementalProcedureList().getLength(); i++) {
                TextArea descriptionTextArea = new TextArea();
                TextArea timeTextArea = new TextArea();
                descriptionTextArea.getStyles().put("font", new Font("Verdana", Font.PLAIN, 12));
                timeTextArea.getStyles().put("font", new Font("Verdana", Font.PLAIN, 12));
                timeTextArea.setEditable(false);
                descriptionTextArea.setEditable(false);
                int hours = p.getStep(i).getDuration().getHours();
                int minutes = p.getStep(i).getDuration().getMinutes();
                String hmTime = "";
                if (hours > 0) {
                    hmTime += String.valueOf(hours) + "h";
                }
                if (minutes > 0) {
                    hmTime += " " + String.valueOf(minutes) + "m";
                }
                timeTextArea.setText(hmTime.trim());
                descriptionTextArea.setText(p.getStep(i).getDescription());
                TablePane.Row row = new TablePane.Row();
                //row.add(titleTextArea);
                row.add(descriptionTextArea);
                row.add(timeTextArea);
                procedureTablePane.getRows().add(row);
                //System.out.println("adding row to procedure");
            }
            TablePane.Row row = new TablePane.Row();
            row.add(new TablePane.Filler());
            int hours = p.getDuration().getHours();
            int minutes = p.getDuration().getMinutes();
            int n = minutes / 60;
            minutes = minutes % 60;
            hours += n;
            String hmTime = "";
            if (hours > 0) {
                hmTime += String.valueOf(hours) + "h";
            }
            if (minutes > 0) {
                hmTime += " " + String.valueOf(minutes) + "m";
            }
            row.add(new Label(hmTime));
            procedureTablePane.getRows().add(row);
        }
    }

    private MenuHandler timeMenuHandler = new MenuHandler.Adapter() {

        @Override
        public boolean configureContextMenu(Component component, Menu menu, int x, int y) {
            menu.getSections().add(procedureContextMenu.getMenuSection());
            return false;
        }
    };


    /**
     * private MenuHandler exampleMenuHandler = new MenuHandler.Adapter() {
     *
     * @Override public boolean configureContextMenu(Component component, Menu menu, int x, int y) {
     * tableContextMenu.setX(x);
     * tableContextMenu.setY(y);
     * menu.getSections().add(tableContextMenu.getMenuSection());
     * return false;
     * }
     * };
     * <p/>
     * <p/>
     * private class TableContextMenu {
     * <p/>
     * private Menu.Section menuSection = new Menu.Section();
     * private Menu.Item addColumnMenuItem = new Menu.Item("Add col");
     * private Menu.Item delColumnMenuItem = new Menu.Item("Remove col");
     * private Menu.Item addRowMenuItem = new Menu.Item("Add row");
     * private Menu.Item delRowMenuItem = new Menu.Item("Remove row");
     * private int x;
     * private int y;
     * <p/>
     * <p/>
     * public void setX(int x) {
     * this.x = x;
     * }
     * <p/>
     * public void setY(int y) {
     * this.y = y;
     * }
     * <p/>
     * public TableContextMenu() {
     * menuSection.add(addColumnMenuItem);
     * menuSection.add(delColumnMenuItem);
     * menuSection.add(addRowMenuItem);
     * menuSection.add(delRowMenuItem);
     * <p/>
     * addColumnMenuItem.setAction(new Action() {
     * @Override public void perform(Component component) {
     * int oldLen = exampleTablePane.getColumns().getLength();
     * System.out.println("adding column");
     * //procedureTablePane.getColumns().remove(1,1);
     * TablePane.Column column = new TablePane.Column();
     * column.setWidth("1*");
     * exampleTablePane.getColumns().add(column);
     * for (int i = oldLen; i < exampleTablePane.getColumns().getLength(); i++ ) {
     * System.out.println("adding row "+i);
     * Label label = new Label();
     * label.setText("adding row "+i);
     * TablePane.Row currentRow = null;
     * if (exampleTablePane.getRows().getLength() == 0) {
     * currentRow = new TablePane.Row();
     * exampleTablePane.getRows().add(currentRow);
     * } else {
     * currentRow = exampleTablePane.getRows().get(exampleTablePane.getRowAt(y));
     * }
     * currentRow.add(label);
     * }
     * }
     * });
     * <p/>
     * delColumnMenuItem.setAction(new Action() {
     * @Override public void perform(Component component) {
     * int icol = exampleTablePane.getColumnAt(x);
     * TablePane.Column column = exampleTablePane.getColumns().get(icol);
     * System.out.println("delete column "+icol + " at "+x);
     * for (int i = 0; i < exampleTablePane.getRows().getLength(); i++) {
     * Component c = exampleTablePane.getCellComponent(i,icol);
     * exampleTablePane.getRows().get(i).remove(c);
     * }
     * exampleTablePane.getColumns().remove(icol,1);
     * }
     * });
     * <p/>
     * addRowMenuItem.setAction(new Action() {
     * @Override public void perform(Component component) {
     * System.out.println("adding row");
     * TablePane.Row row = new TablePane.Row();
     * for (int i = 0; i < exampleTablePane.getColumns().getLength(); i++ ) {
     * System.out.println("adding row "+i);
     * Label label = new Label();
     * label.setText("adding row "+i);
     * row.add(label);
     * }
     * exampleTablePane.getRows().add(row);
     * }
     * });
     * <p/>
     * delRowMenuItem.setAction(new Action() {
     * @Override public void perform(Component component) {
     * System.out.println("deleting row");
     * int rowIndex = exampleTablePane.getRowAt(y);
     * exampleTablePane.getRows().remove(rowIndex,1);
     * }
     * });
     * <p/>
     * }
     * <p/>
     * public Menu.Section getMenuSection() {
     * return menuSection;
     * }
     * }
     */

    private class ProcedureContextMenu {

        private Menu.Section menuSection = new Menu.Section();
        private Menu.Item timeMenuItem = new Menu.Item("Show time");
        private Menu.Item durationMenuItem = new Menu.Item("Show duration");
        private Menu.Item ddtMenuItem = new Menu.Item("Show DDT");

        public ProcedureContextMenu() {
            timeMenuItem.setToggleButton(true);
            durationMenuItem.setToggleButton(true);
            ddtMenuItem.setToggleButton(true);
            menuSection.add(timeMenuItem);
            menuSection.add(durationMenuItem);
            menuSection.add(ddtMenuItem);

            timeMenuItem.setAction(new Action() {
                @Override
                public void perform(Component component) {
                    boolean selected = ((Menu.Item) component).isSelected();
                    if (!selected) {
                        procedureTablePane.getColumns().remove(1, 1);
                    }
                }
            });
        }

        public Menu.Section getMenuSection() {
            return menuSection;
        }
    }

    public void initialize(Map<String, Object> map, URL url, Resources rsrcs) {
        noteTextArea.setEnabled(false);
        procedureContextMenu = new ProcedureContextMenu();
        timeLabel.setMenuHandler(timeMenuHandler);
    }

}
